package com.jkoss.wine.system.controller;

import java.awt.geom.Area;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.BeanToMapUtil;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.system.entity.Areas;
import com.jkoss.wine.system.service.IAreasService;

/**
 * 前端控制器
 * 
 * @Author Jason
 * @Version 1.0, 2018-10-01
 * @See
 * @Since com.jkoss.wine.system.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/system/areas")
public class AreasController extends BaseController {

	@Autowired
	private IAreasService iAreasService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public String list(DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Page resultPage = iAreasService.selectPage(dwzPageBean.toPage(), null);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "system/areas/list";
	}

	/**
	 * 获取省份
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/getProvince", method = RequestMethod.POST)
	@ResponseBody
	public Object getPosCache(HttpServletRequest req) {
		List list = new ArrayList();
		list.add("bj");
		list.add("bj");
		List list1 = new ArrayList();
		list1.add(list);
		return JSON.toJSONString(list1);
	}

	/**
	 * 通过省份获取区/市
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getCityByProvince")
	@ResponseBody
	public Object getCityByProvince(String id, String isSearch, HttpServletRequest request, HttpServletResponse response) {
		List resultList = new ArrayList();
		if (CommonUtil.isBlank(id)) {
			List item = new ArrayList();
			item.add("");
			item.add("请选择");
			resultList.add(item);
			return resultList;
		}
		// 获取省index
		String province = id.substring(0, 2);
		Wrapper wrapper = Condition.create().andNew("id like '" + province + "%00'").ne("id", province + "0000");
		List<Areas> list = iAreasService.selectList(wrapper);
		resultList = getList(list, isSearch);
		return JSON.toJSONString(resultList);
	}

	/**
	 * 通过市获取县城
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getCountyByAreas")
	@ResponseBody
	public Object getCountyByAreas(String id, String isSearch, HttpServletRequest req) {
		List resultList = new ArrayList();
		if (CommonUtil.isBlank(id)) {
			List item = new ArrayList();
			item.add("");
			item.add("请选择");
			resultList.add(item);
			return resultList;
		}
		// 获取省index
		String county = id.substring(0, 4);
		Wrapper wrapper = Condition.create().andNew("id like '" + county + "%'");
		if (!CommonUtil.isBlank(isSearch)) {
			wrapper.ne("id", county + "00");
		}
		List<Areas> list = iAreasService.selectList(wrapper);
		resultList = getList(list, isSearch);
		return JSON.toJSONString(resultList);
	}

	/**
	 * 省市区封装数据
	 * 
	 * @param list1
	 * @param isSearch
	 * @return
	 */
	private List getList(List<Areas> list1, String isSearch) {
		List resultList = new ArrayList();
		if (!CommonUtil.isBlank(isSearch)) {
			List item = new ArrayList();
			item.add("");
			item.add("请选择");
			resultList.add(item);
		}
		for (Areas areas : list1) {
			List item = new ArrayList();
			item.add(areas.getId());
			item.add(areas.getName());
			resultList.add(item);
		}
		return resultList;
	}

}
